package com.heima.config;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class ErrorMessageConfiguration {
    @Bean
    public DirectExchange errorExchange(){
        //申明错误交换机
        return new DirectExchange("error.direct");
    }
    @Bean
    public Queue errorQueue(){
        //生命错误队列
        return new Queue("error.queue");
    }
    @Bean
    public Binding errorBinding(DirectExchange errorExchange, Queue errorQueue){
        //申明绑定关系
        return BindingBuilder.bind(errorQueue).to(errorExchange).with("error");
    }
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        //申明错误策略
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");
    }
}
